package com.prototype.mapping {
	import com.gogetem.game.CollisionMap;
	import com.gogetem.game.Dog;
	import com.gogetem.game.Scenario;
	import com.gogetem.game.TerrainObject;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class MapTest extends Sprite {
		
		private var display:Bitmap;
		private var mainBuffer:BitmapData;
		
		private var scenario:Scenario;
		private var dog:Dog;
		
		public function MapTest() {
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function onAdded(e:Event):void {
			
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			
			//Entry Point
			mainBuffer = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
			scenario = new Scenario();
			dog = new Dog();
			
			display = new Bitmap(mainBuffer);
			//display.x -= 63;
			//display.y -= 600 - display.height;
			addChild(display);
			
			stage.addEventListener(Event.ENTER_FRAME, gameLoop);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
		}
		
		private function onKeyRelease(e:KeyboardEvent):void {
			switch(e.keyCode) {
				case Keyboard.SPACE:
					dog.jump();
					break;
				case Keyboard.LEFT:
					dog.vX--;
					break;
				case Keyboard.RIGHT:
					dog.vX++;
					break;
				case Keyboard.CONTROL:
					dog.attack();
					break;
			}
		}
		
		private function gameLoop(e:Event):void {
			trace("- LOOP")
			updatePhysics();
			render();
		}
		
		private function updatePhysics():void {
			
			var dogColumn:int = Math.ceil((dog.screenPosition.x + dog.buffer.width - 1) / 63);
			var dogRow:int = Math.floor((dog.screenPosition.y + dog.buffer.height - 1) / 63);
			
			var collisionMap:CollisionMap = scenario.getCollisionsAround(dogColumn, dogRow);
		 	switch(scenario.getCollision(collisionMap.center)) {
				case 0:
					if (scenario.getCollision(collisionMap.down) == TerrainObject.EMPTY) {
						dog.vY += 0.4;
					} else {
						dog.vY = Math.min(dog.vY, ((dogRow + 1) * 63) - (dog.screenPosition.y+dog.buffer.height) );
					}
					break;
				case 1:
					dog.screenPosition.y = ((dogRow) * 63) - dog.buffer.height;
					dog.vY = 0;
					break;
				case 2:
					if (dog.vY > 0) {
						dog.vY = 0;
						dog.screenPosition.y -= ((dogRow) * 63) - dog.buffer.height;
					} else {
						if (scenario.getCollision(collisionMap.down) == TerrainObject.EMPTY) {
							dog.vY += 0.4;
						} else {
							dog.vY = Math.min(dog.vY, ((dogRow + 1) * 63) - (dog.screenPosition.y+dog.buffer.height) );
						}
					}
					break;
			}
			
		}
		
		private function render():void {
			
			mainBuffer.fillRect(mainBuffer.rect, 0x00000000);
			scenario.scroll(dog.vX);
			mainBuffer.copyPixels(scenario.buffer, scenario.buffer.rect, new Point( -63, 0), null, null, true);
			dog.update();
			mainBuffer.copyPixels(dog.buffer, dog.buffer.rect, dog.screenPosition, null, null, true);
		}
	}
}